package cn.ljy.mes.service.impl;

import cn.ljy.authority.config.security.UserUtil;
import cn.ljy.authority.dao.mapper.SysFileResourceMapper;
import cn.ljy.authority.model.SysFileResource;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.mes.dao.mapper.MesMaterialMapper;
import cn.ljy.mes.dao.mapper.ShopProductMapper;
import cn.ljy.mes.dao.mapper.ShopProductTypeMapper;
import cn.ljy.mes.model.MesMaterial;
import cn.ljy.mes.model.ShopProduct;
import cn.ljy.mes.model.ShopProductType;
import cn.ljy.mes.model.query.ShopProductQuery;
import cn.ljy.mes.service.ShopProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class IShopProductService extends IBaseService<ShopProduct, ShopProductQuery> implements ShopProductService {
    @Autowired
    private ShopProductMapper mapper;
    @Autowired
    private ShopProductTypeMapper productTypeMapper;
    @Autowired
    private MesMaterialMapper mesMaterialMapper;
    @Autowired
    private SysFileResourceMapper fileResourceMapper;

    @Override
    protected void enrichEntityFields(ShopProduct product){
        if(product!=null){
            //处理封面图片的绝对网址
            SysFileResource coverImage=product.getCoverImage();
            if(coverImage!=null){
                //获取资源的绝对网址
                coverImage.setFileHttpUrl(super.getFileResourceWebUrl(coverImage.getFileUrl()));
            }
        }
    }


    @Override
    protected void beforeSaveOrUpdate(ShopProduct shopProduct) {
        if(shopProduct.getProductType()!=null){//只有传递值过来才进行判断
            if(shopProduct.getProductType().getId()==null){
                throw new MyWebException("验证失败：商品类别id不能为空");
            }else{
                //读取系统记录
                ShopProductType shopProductTypeOriginal=productTypeMapper.findByIdNoRelation(shopProduct.getProductType().getId());
                if(shopProductTypeOriginal==null){
                    throw new MyWebException("验证失败：商品类别不存在");
                }
            }
        }
        if(shopProduct.getMaterial()!=null){
            if(shopProduct.getMaterial().getId()==null){
                throw new MyWebException("验证失败：物料id不能为空");
            }else{
                //读取系统记录
                MesMaterial materialOrignal=mesMaterialMapper.findByIdNoRelation(shopProduct.getMaterial().getId());
                if(materialOrignal==null){
                    throw new MyWebException("验证失败：所选物料不存在");
                }
            }
        }
        if(shopProduct.getCoverImage()!=null) {//只有传递值过来才进行判断
            if(shopProduct.getCoverImage().getId()==null){
                throw new MyWebException("验证失败：封面图片对应的资源id不能为空");
            }else{
                //读取系统记录
                SysFileResource coverImage = fileResourceMapper.findByIdNoRelation(shopProduct.getCoverImage().getId());
                if(coverImage==null){
                    throw new MyWebException("验证失败：所选资源不存在");
                }
            }
        }
    }

    @Override
    protected void beforeSave(ShopProduct shopProduct) {
        //验证失败：促销价格不能大于原价
        if(shopProduct.getPrice().compareTo(shopProduct.getOriginalPrice())==1){
            throw new MyWebException("验证失败：促销价格不能大于原价");
        }
        //设置当前添加的用户信息
        shopProduct.setCreator(UserUtil.getLoginUser());
    }

    @Override
    protected void beforeUpdate(ShopProduct shopProduct) {
        //获取系统原纪录
        ShopProduct productOfOriginal=mapper.findByIdNoRelation(shopProduct.getId());
        if(productOfOriginal==null){
            throw new MyWebException("验证失败：记录不存在");
        }
        //验证失败：促销价格不能大于原价
        BigDecimal price = shopProduct.getPrice() != null ? shopProduct.getPrice() : productOfOriginal.getPrice();
        BigDecimal originalPrice = shopProduct.getOriginalPrice() != null ? shopProduct.getOriginalPrice() : productOfOriginal.getOriginalPrice();
        if (price != null && originalPrice != null && price.compareTo(originalPrice) == 1) {
            throw new MyWebException("验证失败：促销价格不能大于原价");
        }
        //设置当前编辑的用户信息
        shopProduct.setEditor(UserUtil.getLoginUser());
    }


    @Override
    public void updateProductToTransferProductTypeBySelect(String[] productIds,Long productTypeId){
        ShopProductType productType = productTypeMapper.findByIdNoRelation(productTypeId);
        if(productType!=null){
            //进行栏目转移
            mapper.batchTransferProductsToProductType(productIds,productTypeId);
        }else{//如果栏目不存在
            throw new MyWebException("操作错误：栏目不存在");
        }
    }
}
